<?php


namespace app\category\controller;

use app\BaseController;
use app\category\model\Category;

class Index extends BaseController
{
    /**
     * 根据二级id获取商品的一级分类和二级分类
     * @param id $id
     * @return Array --商品分类
     */
    public function index()
    {
        $DBcategory = new Category();
        $id = $this->request->param('id') ? $this->request->param('id') : null;
        if ($id == null) {
            // 查询一级商品分类 
            $category = $DBcategory->where('name', '<>', 'null')->field('id,name')->select();
            foreach ($category as $list) {
                // 根据父级的id查二级商品分类
                $list['sub'] =  $DBcategory->where('pid',  $list['id'])->where('children_name', '<>', 'null')->field('children_id,children_name,children_picture')->select()->toArray();
            }
            return result($category);
        }
        // 如果有id就是根据一级分类id查询该id下的所有二级分类商品
    }
    public function find()
    {
        $DBcategory = new Category();
        $info = $DBcategory->where('name', '<>', 'null')->select();
        return result($info);
    }
}
